﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;

namespace User
{
    /// <summary>
    /// 加密/解密类
    /// </summary>
    public static class Security
    {
        public static byte[] EncryptSHA256(byte[] input)
        {
            var sha256 = SHA256.Create();
            return sha256.ComputeHash(input);
        }
        public static byte[] EncryptMD5(byte[] input)
        {
            var md5 = MD5.Create();
            return md5.ComputeHash(input);
        }

        /// <summary>
        /// 使用HMACSHA512加密数据
        /// </summary>
        /// <param name="input">要加密的数据</param>
        /// <param name="key">加密键</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static string EncryptHMAC512(string input, string key, Encoding encoding)
        {
            var hmca = HMAC.Create("HMACSHA512");
            hmca.Key = encoding.GetBytes(key);
            var data = hmca.ComputeHash(encoding.GetBytes(input));
            return Convert.ToBase64String(data);
        }
        /// <summary>
        /// 验证HMACSHA512加密的数据是否匹配
        /// </summary>
        /// <param name="input">输入数据</param>
        /// <param name="hash_string">要匹配的加密字串</param>
        /// <param name="key">加密键</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static bool VertifyHMAC512(string input, string hash_string, string key, Encoding encoding)
        {
            return EncryptHMAC512(input, key, encoding) == hash_string;
        }
        /// <summary>
        /// 使用AES加密数据
        /// </summary>
        /// <param name="plainText">需要加密的数据</param>
        /// <param name="Key">加密键</param>
        /// <param name="IV">加密向量</param>
        /// <returns></returns>
        public static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (plainText == null || plainText.Length <= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");
            byte[] encrypted;

            // Create an Aes object
            // with the specified key and IV.
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;

                // Create an encryptor to perform the stream transform.
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for encryption.
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            //Write all data to the stream.
                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }
            // Return the encrypted bytes from the memory stream.
            return encrypted;

        }

        /// <summary>
        /// 使用AES解密数据
        /// </summary>
        /// <param name="cipherText">需要解密的数据</param>
        /// <param name="Key">加密键</param>
        /// <param name="IV">加密向量</param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV, Encoding encoding)
        {
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an Aes object
            // with the specified key and IV.
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;

                // Create a decryptor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt, encoding: encoding))
                        {
                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }

            }

            return plaintext;

        }
    }
}
